﻿using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using log4net;
using System.Reflection;
using System.Configuration;
using System.Net;
using System.Text;

namespace AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service
{
   public class TintingDataSyncService
    {
        /// <summary>
        /// 日志记录
        /// </summary>
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static List<TintingSyncModel> GetSyncList()
        {
            List<TintingSyncModel> authIdList = new List<TintingSyncModel>();
            try
            {
                using (var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
                {
                    authIdList = conn.Query<TintingSyncModel>("SELECT * FROM t_smd_tinting_sync where sync_status <> 2 and sync_times <= 5").ToList();
                }

            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }
            return authIdList;
        }

        /// <summary>
        /// 将List<CrmSyncModel>转换成json
        /// </summary>
        /// <param name="syncList">同步数据</param>
        /// <returns></returns>
        public static string List2Json(List<TintingSyncModel> syncList)
        {
            string jsonString = string.Empty;

            try
            {
                if (syncList.Count > 0)
                {
                    jsonString = JsonHelper.ListToJson<TintingSyncModel>(syncList, "data");
                }
            }

            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }

            return jsonString;
        }


        /// <summary>
        /// Post数据到CRM接口
        /// </summary>
        /// <param name="postData">post数据 json格式</param>
        /// <returns></returns>
        public static HttpResult RestPost(string postData)
        {
            HttpResult result = new HttpResult();
            HttpHelper hpHelper = new HttpHelper();
            HttpItem item = new HttpItem();
            item.URL = ConfigurationManager.AppSettings["TinTingSyncApiAddress"];
            item.Method = "POST";
            item.PostEncoding = Encoding.UTF8;
            item.Postdata = postData;
            item.ContentType = "application/json";

            ICredentials cred = new NetworkCredential(ConfigurationManager.AppSettings["TinTingCredentialUser"], ConfigurationManager.AppSettings["TinTingCredentialPassword"]);
            item.ICredentials = cred;

            result = hpHelper.GetHtml(item);


            return result;

        }

        /// <summary>
        /// 更新同步状态
        /// </summary>
        /// <param name="syncModelList">同步数据</param>
        /// <param name="syncStatus">同步状态</param>
        /// <param name="responseMsg">同步结果信息</param>
        public static void UpdateSyncStatus(List<TintingSyncModel> syncModelList, SyncStatus syncStatus, string responseMsg)
        {
            try
            {
                using (var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
                {
                    foreach (TintingSyncModel syncModel in syncModelList)
                    {
                        conn.Execute("update t_smd_tinting_sync set sync_status =@SyncStatu, sync_times =@SyncTimes, return_msg =@ReturnMsg ",
                        new { SyncStatu = syncStatus, SyncTimes = syncModel.sync_times + 1, ReturnMsg = responseMsg });
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }

        }
    }
}
